package com.example.demoeventspring.listener;


import com.example.demoeventspring.event.LoginNotifyEvent;
import com.example.demoeventspring.event.RegisterNotifyEvent;
import com.example.demoeventspring.service.EventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步事件监听器
 */
@Slf4j
@Component
public class AsyncEventListener {
    @Autowired
    private EventService eventService;

    /**
     * 监听 注册通知
     *
     * @param myEvent
     */
    @EventListener
    @Async("AsyncTaskExecutor")
    public void RegisterNotifyEvent(RegisterNotifyEvent myEvent) {
        log.info("线程：{} 执行了MyEvent事件", Thread.currentThread().getName());
        // 具体的业务逻辑过程
        this.eventService.RegisterNotifyEvent(myEvent);
    }

    /**
     * 监听 登录通知
     *
     * @param anotherEvent
     */
    @EventListener
    @Async("AsyncTaskExecutor")
    public void LoginNotifyEvent(LoginNotifyEvent anotherEvent) {
        log.info("线程：{} 执行了AnotherEvent事件", Thread.currentThread().getName());
        // 具体的业务逻辑过程
        this.eventService.LoginNotifyEvent(anotherEvent);
    }

}
